package io.zt.dangyuan;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import io.zt.dangyuan.object.ContentListObject;
import io.zt.dangyuan.util.Cons;

import com.bumptech.glide.Glide;
import com.orhanobut.logger.Logger;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.ex.HttpException;

import java.util.HashMap;
import java.util.List;

public class BBSItemAdapter1 extends BaseAdapter {
	private  LayoutInflater mInflater;
	private List<ContentListObject.ListEntity> datas;
	private Context context;
//	private   BitmapUtils bitmapUtils;
	private String url = "content.json?";


//    private  bitmapUtils.configMemoryCacheEnabled(true);

	public BBSItemAdapter1(Context context, List<ContentListObject.ListEntity> datas) {
		super();
		this.context=context;
		this.datas=datas;
		this.mInflater = LayoutInflater.from(context);
//		bitmapUtils = new BitmapUtils(context);
//		bitmapUtils.configMemoryCacheEnabled(true);
//		bitmapUtils.configDefaultLoadingImage(R.drawable.icon_default);
//		bitmapUtils.configDefaultLoadFailedImage(R.drawable.icon_default);

	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return datas.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return datas.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder=new ViewHolder();
			convertView = mInflater.inflate(R.layout.bbs_main_item, null);
			holder.iv_itemImag = (ImageView) convertView.findViewById(R.id.bbs_item_img);
			holder.tv_itemTitle = (TextView) convertView.findViewById(R.id.bbs_item_title);
			holder.tv_itemTime = (TextView) convertView.findViewById(R.id.bbs_item_time);
			holder.tv_itemState = (TextView) convertView.findViewById(R.id.bbs_item_count);
			holder.iv_image_share= (ImageView) convertView.findViewById(R.id.image_share);
			convertView.setTag(holder);
		}else {
			holder = (ViewHolder)convertView.getTag();
		}
		final ContentListObject.ListEntity object =  datas.get(position);
//		BitmapUtils bitmapUtils = new BitmapUtils(context);
//		bitmapUtils.configMemoryCacheEnabled(true);
//		bitmapUtils.configDefaultLoadingImage(R.drawable.icon_default);
//		bitmapUtils.configDefaultLoadFailedImage(R.drawable.icon_default);
//		bitmapUtils.display(holder.iv_itemImag, Cons.BASE_URL + object.getTitleImg());

		Glide.with(context).load(Cons.BASE_URL + object.getTitleImg()).placeholder(R.drawable.icon_default).into(holder.iv_itemImag);

		holder.tv_itemTitle.setText(object.getTitle());
		holder.tv_itemTime.setText(object.getPubTime());
		holder.tv_itemState.setText(String.valueOf(object.getClicks()));
		holder.iv_image_share.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
			url+="contentid"+"="+object.getArcId()+"&cid=hdfb";
				WebUtils.sendPost(context, url, new HashMap<String,Object>(),new WebUtils.OnWebListenr() {
					@Override
					public void onSuccess(boolean isSuccess, String msg, String data) {
						Logger.i("lai  huxiang shanghai",url);
						JSONObject jsonObject = null;
						try {
							jsonObject = new JSONObject(data);
							JSONObject json = jsonObject.getJSONObject("data");
							String targeturl=json.optString("url");
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}

					@Override
					public void onFailed(HttpException error, String msg) {

					}
				});


			}
		});
		return convertView;
	}

	class ViewHolder   {
		TextView tv_itemTitle;
		TextView tv_itemTime;
		TextView tv_itemState;
		ImageView iv_itemImag;
		ImageView iv_image_share;
	}





}